<!DOCTYPE html>
<html lang="zh-CN" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>javase | ╭(╯^╰)╮</title>
    <meta name="description" content="王遂杨的卷王日常">
    <link rel="preload stylesheet" href="/scroll-kings-notes/assets/style.ab3cf6e4.css" as="style">
    
    <script type="module" src="/scroll-kings-notes/assets/app.c1828823.js"></script>
    <link rel="preload" href="/scroll-kings-notes/assets/inter-roman-latin.2ed14f66.woff2" as="font" type="font/woff2" crossorigin="">
    <link rel="modulepreload" href="/scroll-kings-notes/assets/chunks/framework.4760b438.js">
    <link rel="modulepreload" href="/scroll-kings-notes/assets/chunks/theme.507f5f7c.js">
    <link rel="modulepreload" href="/scroll-kings-notes/assets/javase.md.05ebba13.lean.js">
    <link rel="icon" href="/wsy.ico">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css">
    <script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui@4.0/dist/fancybox.umd.js"></script>
    <script id="check-dark-light">(()=>{const e=localStorage.getItem("vitepress-theme-appearance")||"auto",a=window.matchMedia("(prefers-color-scheme: dark)").matches;(!e||e==="auto"?a:e==="dark")&&document.documentElement.classList.add("dark")})();</script>
    <link rel="manifest" href="/scroll-kings-notes/manifest.webmanifest">
    <script id="vite-plugin-pwa:register-sw" src="/scroll-kings-notes/registerSW.js"></script>
  </head>
  <body>
    <div id="app"><div class="Layout" data-v-88c5167e><!--[--><!--]--><!--[--><span tabindex="-1" data-v-fa9bda99></span><a href="#VPContent" class="VPSkipLink visually-hidden" data-v-fa9bda99> Skip to content </a><!--]--><!----><header class="VPNav" data-v-88c5167e data-v-9a976e98><div class="VPNavBar has-sidebar" data-v-9a976e98 data-v-cbeb2402><div class="container" data-v-cbeb2402><div class="title" data-v-cbeb2402><div class="VPNavBarTitle has-sidebar" data-v-cbeb2402 data-v-f35ad59b><a class="title" href="/scroll-kings-notes/" data-v-f35ad59b><!--[--><!--]--><!--[--><img class="VPImage logo" src="/scroll-kings-notes/wsy.png" alt data-v-9395b6f7><!--]--><!--[-->天不生我王遂杨卷道万古如长夜<!--]--><!--[--><!--]--></a></div></div><div class="content" data-v-cbeb2402><div class="curtain" data-v-cbeb2402></div><div class="content-body" data-v-cbeb2402><!--[--><!--]--><div class="VPNavBarSearch search" style="--vp-meta-key:&#39;Meta&#39;;" data-v-cbeb2402><!--[--><!----><div id="local-search"><button type="button" class="DocSearch DocSearch-Button" aria-label="Search"><span class="DocSearch-Button-Container"><svg class="DocSearch-Search-Icon" width="20" height="20" viewBox="0 0 20 20" aria-label="search icon"><path d="M14.386 14.386l4.0877 4.0877-4.0877-4.0877c-2.9418 2.9419-7.7115 2.9419-10.6533 0-2.9419-2.9418-2.9419-7.7115 0-10.6533 2.9418-2.9419 7.7115-2.9419 10.6533 0 2.9419 2.9418 2.9419 7.7115 0 10.6533z" stroke="currentColor" fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"></path></svg><span class="DocSearch-Button-Placeholder">搜索文档</span></span><span class="DocSearch-Button-Keys"><kbd class="DocSearch-Button-Key"></kbd><kbd class="DocSearch-Button-Key">K</kbd></span></button></div><!--]--></div><nav aria-labelledby="main-nav-aria-label" class="VPNavBarMenu menu" data-v-cbeb2402 data-v-25cfc4fa><span id="main-nav-aria-label" class="visually-hidden" data-v-25cfc4fa>Main Navigation</span><!--[--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-25cfc4fa data-v-bd0fa5dd><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-bd0fa5dd><span class="text" data-v-bd0fa5dd><!----><span data-v-bd0fa5dd>卷王笔记ヾ(•ω•`)o</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-bd0fa5dd><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-bd0fa5dd><div class="VPMenu" data-v-bd0fa5dd data-v-6998faec><div class="items" data-v-6998faec><!--[--><!--[--><div class="VPMenuLink" data-v-6998faec data-v-2628fae8><a class="VPLink link active" href="/scroll-kings-notes/javase.html" data-v-2628fae8><!--[-->JavaSE<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-6998faec data-v-2628fae8><a class="VPLink link" href="/scroll-kings-notes/javaweb.html" data-v-2628fae8><!--[-->JavaWeb<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-6998faec data-v-2628fae8><a class="VPLink link" href="/scroll-kings-notes/web.html" data-v-2628fae8><!--[-->前端<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-6998faec data-v-2628fae8><a class="VPLink link" href="/scroll-kings-notes/cangqiong.html" data-v-2628fae8><!--[-->苍穹外卖<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-6998faec data-v-2628fae8><a class="VPLink link" href="/scroll-kings-notes/mybatisplus.html" data-v-2628fae8><!--[-->MybatisPlus<!--]--></a></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/scroll-kings-notes/life.html" tabindex="0" data-v-25cfc4fa data-v-980e37eb><!--[--><span data-v-980e37eb>卷王日常(￣o￣).zZ</span><!--]--></a><!--]--><!--]--></nav><!----><div class="VPNavBarAppearance appearance" data-v-cbeb2402 data-v-539d8d76><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title="toggle dark mode" aria-checked="false" data-v-539d8d76 data-v-67cabb88 data-v-13676b0f><span class="check" data-v-13676b0f><span class="icon" data-v-13676b0f><!--[--><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="sun" data-v-67cabb88><path d="M12,18c-3.3,0-6-2.7-6-6s2.7-6,6-6s6,2.7,6,6S15.3,18,12,18zM12,8c-2.2,0-4,1.8-4,4c0,2.2,1.8,4,4,4c2.2,0,4-1.8,4-4C16,9.8,14.2,8,12,8z"></path><path d="M12,4c-0.6,0-1-0.4-1-1V1c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,3.6,12.6,4,12,4z"></path><path d="M12,24c-0.6,0-1-0.4-1-1v-2c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,23.6,12.6,24,12,24z"></path><path d="M5.6,6.6c-0.3,0-0.5-0.1-0.7-0.3L3.5,4.9c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C6.2,6.5,5.9,6.6,5.6,6.6z"></path><path d="M19.8,20.8c-0.3,0-0.5-0.1-0.7-0.3l-1.4-1.4c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C20.3,20.7,20,20.8,19.8,20.8z"></path><path d="M3,13H1c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S3.6,13,3,13z"></path><path d="M23,13h-2c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S23.6,13,23,13z"></path><path d="M4.2,20.8c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C4.7,20.7,4.5,20.8,4.2,20.8z"></path><path d="M18.4,6.6c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C18.9,6.5,18.6,6.6,18.4,6.6z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="moon" data-v-67cabb88><path d="M12.1,22c-0.3,0-0.6,0-0.9,0c-5.5-0.5-9.5-5.4-9-10.9c0.4-4.8,4.2-8.6,9-9c0.4,0,0.8,0.2,1,0.5c0.2,0.3,0.2,0.8-0.1,1.1c-2,2.7-1.4,6.4,1.3,8.4c2.1,1.6,5,1.6,7.1,0c0.3-0.2,0.7-0.3,1.1-0.1c0.3,0.2,0.5,0.6,0.5,1c-0.2,2.7-1.5,5.1-3.6,6.8C16.6,21.2,14.4,22,12.1,22zM9.3,4.4c-2.9,1-5,3.6-5.2,6.8c-0.4,4.4,2.8,8.3,7.2,8.7c2.1,0.2,4.2-0.4,5.8-1.8c1.1-0.9,1.9-2.1,2.4-3.4c-2.5,0.9-5.3,0.5-7.5-1.1C9.2,11.4,8.1,7.7,9.3,4.4z"></path></svg><!--]--></span></span></button></div><div class="VPSocialLinks VPNavBarSocialLinks social-links" data-v-cbeb2402 data-v-c595312f data-v-756fd4a4><!--[--><a class="VPSocialLink no-icon" href aria-label target="_blank" rel="noopener" data-v-756fd4a4 data-v-54de494a><svg t="1708159775126" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="10613" width="200" height="200"><path d="M669.3 369.4c9.8 0 19.6 0 29.4 1.6C671 245.2 536.9 152 383.2 152 211.6 152 71 269.7 71 416.8c0 85 45.8 156.9 124.2 210.9l-31.1 93.2L273.6 667c39.2 8.2 70.3 16.3 109.5 16.3 9.8 0 19.6 0 31.1-1.6-6.5-21.3-9.8-42.5-9.8-65.4 0.1-135.7 116.2-246.9 264.9-246.9z m-168.4-85c24.5 0 39.2 16.3 39.2 39.2 0 22.9-16.3 39.2-39.2 39.2-24.5 0-47.4-16.4-47.4-39.2 0-24.5 24.6-39.2 47.4-39.2z m-216.3 73.1c-24.7 0-47.8-16.2-47.8-38.8 0-24.3 24.7-38.8 47.8-38.8s39.5 16.2 39.5 38.8c0.1 22.7-16.4 38.8-39.5 38.8z" fill="#24DB5A" p-id="10614"></path><path d="M953.8 613c0-125.9-124.2-227.2-264.8-227.2-148.8 0-266.5 103-266.5 227.2 0 125.9 117.7 227.2 266.5 227.2 31.1 0 62.1-8.2 93.2-16.3l85 47.4-22.9-78.5c62.1-47.4 109.5-109.5 109.5-179.8z m-351.5-39.2c-14.7 0-31.1-14.7-31.1-31.1 0-14.7 16.3-31.1 31.1-31.1 22.9 0 39.2 16.3 39.2 31.1 0 16.4-14.7 31.1-39.2 31.1z m178-7.6c-14.8 0-31.3-14.6-31.3-30.7 0-14.6 16.5-30.7 31.3-30.7 23.1 0 39.5 16.2 39.5 30.7 0 16.2-16.4 30.7-39.5 30.7z" fill="#24DB5A" p-id="10615"></path></svg></a><a class="VPSocialLink no-icon" href aria-label target="_blank" rel="noopener" data-v-756fd4a4 data-v-54de494a><svg t="1708159621014" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="8757" width="200" height="200"><path d="M512 1024C229.222 1024 0 794.778 0 512S229.222 0 512 0s512 229.222 512 512-229.222 512-512 512z m259.149-568.883h-290.74a25.293 25.293 0 0 0-25.292 25.293l-0.026 63.206c0 13.952 11.315 25.293 25.267 25.293h177.024c13.978 0 25.293 11.315 25.293 25.267v12.646a75.853 75.853 0 0 1-75.853 75.853h-240.23a25.293 25.293 0 0 1-25.267-25.293V417.203a75.853 75.853 0 0 1 75.827-75.853h353.946a25.293 25.293 0 0 0 25.267-25.292l0.077-63.207a25.293 25.293 0 0 0-25.268-25.293H417.152a189.62 189.62 0 0 0-189.62 189.645V771.15c0 13.977 11.316 25.293 25.294 25.293h372.94a170.65 170.65 0 0 0 170.65-170.65V480.384a25.293 25.293 0 0 0-25.293-25.267z" fill="#C71D23" p-id="8758"></path></svg></a><a class="VPSocialLink no-icon" href aria-label target="_blank" rel="noopener" data-v-756fd4a4 data-v-54de494a><svg t="1708159909604" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="12483" width="200" height="200"><path d="M512 0a512 512 0 1 0 512 512A512 512 0 0 0 512 0z m256.717 460.186a151.962 151.962 0 0 1-87.347 65.74 83.251 83.251 0 0 1-24.474 4.096 29.082 29.082 0 0 1 0-58.163 15.667 15.667 0 0 0 6.451-1.229 91.443 91.443 0 0 0 55.91-40.96 75.264 75.264 0 0 0 11.06-39.628c0-45.978-42.496-83.866-94.31-83.866a105.267 105.267 0 0 0-51.2 13.414 81.92 81.92 0 0 0-43.725 70.452v244.224a138.445 138.445 0 0 1-72.704 120.422 159.642 159.642 0 0 1-79.77 20.48c-84.378 0-153.6-63.488-153.6-142.029a136.192 136.192 0 0 1 19.763-69.837 151.962 151.962 0 0 1 87.347-65.74 85.914 85.914 0 0 1 24.474-4.096 29.082 29.082 0 1 1 0 58.163 15.667 15.667 0 0 0-6.451 1.229 95.949 95.949 0 0 0-55.91 40.96 75.264 75.264 0 0 0-11.06 39.628c0 45.978 42.496 83.866 94.925 83.866a105.267 105.267 0 0 0 51.2-13.414 81.92 81.92 0 0 0 43.622-70.452V390.35a138.752 138.752 0 0 1 72.807-120.525 151.245 151.245 0 0 1 79.155-21.504c84.378 0 153.6 63.488 153.6 142.029a136.192 136.192 0 0 1-19.763 69.837z" fill="#00B240" p-id="12484"></path></svg></a><!--]--></div><div class="VPFlyout VPNavBarExtra extra" data-v-cbeb2402 data-v-2d421a04 data-v-bd0fa5dd><button type="button" class="button" aria-haspopup="true" aria-expanded="false" aria-label="extra navigation" data-v-bd0fa5dd><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="icon" data-v-bd0fa5dd><circle cx="12" cy="12" r="2"></circle><circle cx="19" cy="12" r="2"></circle><circle cx="5" cy="12" r="2"></circle></svg></button><div class="menu" data-v-bd0fa5dd><div class="VPMenu" data-v-bd0fa5dd data-v-6998faec><!----><!--[--><!--[--><!----><div class="group" data-v-2d421a04><div class="item appearance" data-v-2d421a04><p class="label" data-v-2d421a04>Appearance</p><div class="appearance-action" data-v-2d421a04><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title="toggle dark mode" aria-checked="false" data-v-2d421a04 data-v-67cabb88 data-v-13676b0f><span class="check" data-v-13676b0f><span class="icon" data-v-13676b0f><!--[--><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="sun" data-v-67cabb88><path d="M12,18c-3.3,0-6-2.7-6-6s2.7-6,6-6s6,2.7,6,6S15.3,18,12,18zM12,8c-2.2,0-4,1.8-4,4c0,2.2,1.8,4,4,4c2.2,0,4-1.8,4-4C16,9.8,14.2,8,12,8z"></path><path d="M12,4c-0.6,0-1-0.4-1-1V1c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,3.6,12.6,4,12,4z"></path><path d="M12,24c-0.6,0-1-0.4-1-1v-2c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,23.6,12.6,24,12,24z"></path><path d="M5.6,6.6c-0.3,0-0.5-0.1-0.7-0.3L3.5,4.9c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C6.2,6.5,5.9,6.6,5.6,6.6z"></path><path d="M19.8,20.8c-0.3,0-0.5-0.1-0.7-0.3l-1.4-1.4c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C20.3,20.7,20,20.8,19.8,20.8z"></path><path d="M3,13H1c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S3.6,13,3,13z"></path><path d="M23,13h-2c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S23.6,13,23,13z"></path><path d="M4.2,20.8c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C4.7,20.7,4.5,20.8,4.2,20.8z"></path><path d="M18.4,6.6c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C18.9,6.5,18.6,6.6,18.4,6.6z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="moon" data-v-67cabb88><path d="M12.1,22c-0.3,0-0.6,0-0.9,0c-5.5-0.5-9.5-5.4-9-10.9c0.4-4.8,4.2-8.6,9-9c0.4,0,0.8,0.2,1,0.5c0.2,0.3,0.2,0.8-0.1,1.1c-2,2.7-1.4,6.4,1.3,8.4c2.1,1.6,5,1.6,7.1,0c0.3-0.2,0.7-0.3,1.1-0.1c0.3,0.2,0.5,0.6,0.5,1c-0.2,2.7-1.5,5.1-3.6,6.8C16.6,21.2,14.4,22,12.1,22zM9.3,4.4c-2.9,1-5,3.6-5.2,6.8c-0.4,4.4,2.8,8.3,7.2,8.7c2.1,0.2,4.2-0.4,5.8-1.8c1.1-0.9,1.9-2.1,2.4-3.4c-2.5,0.9-5.3,0.5-7.5-1.1C9.2,11.4,8.1,7.7,9.3,4.4z"></path></svg><!--]--></span></span></button></div></div></div><div class="group" data-v-2d421a04><div class="item social-links" data-v-2d421a04><div class="VPSocialLinks social-links-list" data-v-2d421a04 data-v-756fd4a4><!--[--><a class="VPSocialLink no-icon" href aria-label target="_blank" rel="noopener" data-v-756fd4a4 data-v-54de494a><svg t="1708159775126" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="10613" width="200" height="200"><path d="M669.3 369.4c9.8 0 19.6 0 29.4 1.6C671 245.2 536.9 152 383.2 152 211.6 152 71 269.7 71 416.8c0 85 45.8 156.9 124.2 210.9l-31.1 93.2L273.6 667c39.2 8.2 70.3 16.3 109.5 16.3 9.8 0 19.6 0 31.1-1.6-6.5-21.3-9.8-42.5-9.8-65.4 0.1-135.7 116.2-246.9 264.9-246.9z m-168.4-85c24.5 0 39.2 16.3 39.2 39.2 0 22.9-16.3 39.2-39.2 39.2-24.5 0-47.4-16.4-47.4-39.2 0-24.5 24.6-39.2 47.4-39.2z m-216.3 73.1c-24.7 0-47.8-16.2-47.8-38.8 0-24.3 24.7-38.8 47.8-38.8s39.5 16.2 39.5 38.8c0.1 22.7-16.4 38.8-39.5 38.8z" fill="#24DB5A" p-id="10614"></path><path d="M953.8 613c0-125.9-124.2-227.2-264.8-227.2-148.8 0-266.5 103-266.5 227.2 0 125.9 117.7 227.2 266.5 227.2 31.1 0 62.1-8.2 93.2-16.3l85 47.4-22.9-78.5c62.1-47.4 109.5-109.5 109.5-179.8z m-351.5-39.2c-14.7 0-31.1-14.7-31.1-31.1 0-14.7 16.3-31.1 31.1-31.1 22.9 0 39.2 16.3 39.2 31.1 0 16.4-14.7 31.1-39.2 31.1z m178-7.6c-14.8 0-31.3-14.6-31.3-30.7 0-14.6 16.5-30.7 31.3-30.7 23.1 0 39.5 16.2 39.5 30.7 0 16.2-16.4 30.7-39.5 30.7z" fill="#24DB5A" p-id="10615"></path></svg></a><a class="VPSocialLink no-icon" href aria-label target="_blank" rel="noopener" data-v-756fd4a4 data-v-54de494a><svg t="1708159621014" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="8757" width="200" height="200"><path d="M512 1024C229.222 1024 0 794.778 0 512S229.222 0 512 0s512 229.222 512 512-229.222 512-512 512z m259.149-568.883h-290.74a25.293 25.293 0 0 0-25.292 25.293l-0.026 63.206c0 13.952 11.315 25.293 25.267 25.293h177.024c13.978 0 25.293 11.315 25.293 25.267v12.646a75.853 75.853 0 0 1-75.853 75.853h-240.23a25.293 25.293 0 0 1-25.267-25.293V417.203a75.853 75.853 0 0 1 75.827-75.853h353.946a25.293 25.293 0 0 0 25.267-25.292l0.077-63.207a25.293 25.293 0 0 0-25.268-25.293H417.152a189.62 189.62 0 0 0-189.62 189.645V771.15c0 13.977 11.316 25.293 25.294 25.293h372.94a170.65 170.65 0 0 0 170.65-170.65V480.384a25.293 25.293 0 0 0-25.293-25.267z" fill="#C71D23" p-id="8758"></path></svg></a><a class="VPSocialLink no-icon" href aria-label target="_blank" rel="noopener" data-v-756fd4a4 data-v-54de494a><svg t="1708159909604" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="12483" width="200" height="200"><path d="M512 0a512 512 0 1 0 512 512A512 512 0 0 0 512 0z m256.717 460.186a151.962 151.962 0 0 1-87.347 65.74 83.251 83.251 0 0 1-24.474 4.096 29.082 29.082 0 0 1 0-58.163 15.667 15.667 0 0 0 6.451-1.229 91.443 91.443 0 0 0 55.91-40.96 75.264 75.264 0 0 0 11.06-39.628c0-45.978-42.496-83.866-94.31-83.866a105.267 105.267 0 0 0-51.2 13.414 81.92 81.92 0 0 0-43.725 70.452v244.224a138.445 138.445 0 0 1-72.704 120.422 159.642 159.642 0 0 1-79.77 20.48c-84.378 0-153.6-63.488-153.6-142.029a136.192 136.192 0 0 1 19.763-69.837 151.962 151.962 0 0 1 87.347-65.74 85.914 85.914 0 0 1 24.474-4.096 29.082 29.082 0 1 1 0 58.163 15.667 15.667 0 0 0-6.451 1.229 95.949 95.949 0 0 0-55.91 40.96 75.264 75.264 0 0 0-11.06 39.628c0 45.978 42.496 83.866 94.925 83.866a105.267 105.267 0 0 0 51.2-13.414 81.92 81.92 0 0 0 43.622-70.452V390.35a138.752 138.752 0 0 1 72.807-120.525 151.245 151.245 0 0 1 79.155-21.504c84.378 0 153.6 63.488 153.6 142.029a136.192 136.192 0 0 1-19.763 69.837z" fill="#00B240" p-id="12484"></path></svg></a><!--]--></div></div></div><!--]--><!--]--></div></div></div><!--[--><!--]--><button type="button" class="VPNavBarHamburger hamburger" aria-label="mobile navigation" aria-expanded="false" aria-controls="VPNavScreen" data-v-cbeb2402 data-v-5f2d0f92><span class="container" data-v-5f2d0f92><span class="top" data-v-5f2d0f92></span><span class="middle" data-v-5f2d0f92></span><span class="bottom" data-v-5f2d0f92></span></span></button></div></div></div></div><!----></header><div class="VPLocalNav reached-top" data-v-88c5167e data-v-10746261><button class="menu" aria-expanded="false" aria-controls="VPSidebarNav" data-v-10746261><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="menu-icon" data-v-10746261><path d="M17,11H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h14c0.6,0,1,0.4,1,1S17.6,11,17,11z"></path><path d="M21,7H3C2.4,7,2,6.6,2,6s0.4-1,1-1h18c0.6,0,1,0.4,1,1S21.6,7,21,7z"></path><path d="M21,15H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h18c0.6,0,1,0.4,1,1S21.6,15,21,15z"></path><path d="M17,19H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h14c0.6,0,1,0.4,1,1S17.6,19,17,19z"></path></svg><span class="menu-text" data-v-10746261>Menu</span></button><div class="VPLocalNavOutlineDropdown" style="--vp-vh:0px;" data-v-10746261 data-v-0f7193fb><button data-v-0f7193fb>返回顶部</button><!----></div></div><aside class="VPSidebar" data-v-88c5167e data-v-b66064aa><div class="curtain" data-v-b66064aa></div><nav class="nav" id="VPSidebarNav" aria-labelledby="sidebar-aria-label" tabindex="-1" data-v-b66064aa><span class="visually-hidden" id="sidebar-aria-label" data-v-b66064aa> Sidebar Navigation </span><!--[--><!--]--><!--[--><div class="group" data-v-b66064aa><section class="VPSidebarItem level-0 has-active" data-v-b66064aa data-v-eeeffe62><div class="item" role="button" tabindex="0" data-v-eeeffe62><div class="indicator" data-v-eeeffe62></div><h2 class="text" data-v-eeeffe62>看我王遂杨卷死你</h2><!----></div><div class="items" data-v-eeeffe62><!--[--><div class="VPSidebarItem level-1 is-link" data-v-eeeffe62 data-v-eeeffe62><div class="item" data-v-eeeffe62><div class="indicator" data-v-eeeffe62></div><a class="VPLink link link" href="/scroll-kings-notes/javase.html" data-v-eeeffe62><!--[--><p class="text" data-v-eeeffe62>JavaSE</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-eeeffe62 data-v-eeeffe62><div class="item" data-v-eeeffe62><div class="indicator" data-v-eeeffe62></div><a class="VPLink link link" href="/scroll-kings-notes/javaweb.html" data-v-eeeffe62><!--[--><p class="text" data-v-eeeffe62>JavaWeb</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-eeeffe62 data-v-eeeffe62><div class="item" data-v-eeeffe62><div class="indicator" data-v-eeeffe62></div><a class="VPLink link link" href="/scroll-kings-notes/web.html" data-v-eeeffe62><!--[--><p class="text" data-v-eeeffe62>前端</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-eeeffe62 data-v-eeeffe62><div class="item" data-v-eeeffe62><div class="indicator" data-v-eeeffe62></div><a class="VPLink link link" href="/scroll-kings-notes/cangqiong.html" data-v-eeeffe62><!--[--><p class="text" data-v-eeeffe62>苍穹外卖</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-eeeffe62 data-v-eeeffe62><div class="item" data-v-eeeffe62><div class="indicator" data-v-eeeffe62></div><a class="VPLink link link" href="/scroll-kings-notes/mybatisplus.html" data-v-eeeffe62><!--[--><p class="text" data-v-eeeffe62>MybatisPlus</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><!--]--><!--[--><!--]--></nav></aside><div class="VPContent has-sidebar" id="VPContent" data-v-88c5167e data-v-c50f4b33><div class="VPDoc has-sidebar has-aside" data-v-c50f4b33 data-v-79724263><!--[--><!--]--><div class="container" data-v-79724263><div class="aside" data-v-79724263><div class="aside-curtain" data-v-79724263></div><div class="aside-container" data-v-79724263><div class="aside-content" data-v-79724263><div class="VPDocAside" data-v-79724263 data-v-21147433><!--[--><!--]--><!--[--><!--]--><div class="VPDocAsideOutline" role="navigation" data-v-21147433 data-v-acf31363><div class="content" data-v-acf31363><div class="outline-marker" data-v-acf31363></div><div class="outline-title" role="heading" data-v-acf31363>目录</div><nav aria-labelledby="doc-outline-aria-label" data-v-acf31363><span class="visually-hidden" id="doc-outline-aria-label" data-v-acf31363> Table of Contents for current page </span><ul class="root" data-v-acf31363 data-v-0075ffdf><!--[--><!--]--></ul></nav></div></div><!--[--><!--]--><div class="spacer" data-v-21147433></div><!--[--><!--]--><!----><!--[--><!--]--><!--[--><!--]--></div></div></div></div><div class="content" data-v-79724263><div class="content-container" data-v-79724263><!--[--><!--]--><!----><main class="main" data-v-79724263><div style="position:relative;" class="vp-doc _scroll-kings-notes_javase" data-v-79724263><div><h1 id="javase" tabindex="-1">javase <a class="header-anchor" href="#javase" aria-label="Permalink to &quot;javase&quot;">​</a></h1><h2 id="string" tabindex="-1">String <a class="header-anchor" href="#string" aria-label="Permalink to &quot;String&quot;">​</a></h2><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">charAt</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">i</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> 获取第i个字符</span></span>
<span class="line"><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> res </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> s</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">substring</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">begin</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;">end</span><span style="color:#89DDFF;">);</span><span style="color:#BABED8;"> 截取字符串</span></span>
<span class="line"><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> res </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> s</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">replace</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">old</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#89DDFF;">);</span><span style="color:#BABED8;"> 替换</span></span>
<span class="line"><span style="color:#82AAFF;">toCharArray</span><span style="color:#89DDFF;">()</span><span style="color:#BABED8;"> 转换为字符数组</span></span>
<span class="line"><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> res </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> s</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">split</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">*</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span><span style="color:#BABED8;">按字符分割为数组</span></span>
<span class="line"><span style="color:#BABED8;">str1</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">compareTo</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">str2</span><span style="color:#89DDFF;">);</span><span style="color:#BABED8;"> 两个字符串比大小</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h2 id="stringbuilder" tabindex="-1">StringBuilder <a class="header-anchor" href="#stringbuilder" aria-label="Permalink to &quot;StringBuilder&quot;">​</a></h2><blockquote><p>new Stringbuilder().append().reverse(); 拼接反转</p></blockquote><h2 id="权限修饰符" tabindex="-1">权限修饰符 <a class="header-anchor" href="#权限修饰符" aria-label="Permalink to &quot;权限修饰符&quot;">​</a></h2><p><img src="/scroll-kings-notes/assets/1707822892015.4dc5bc60.png" alt="70782289201" data-fancybox="gallery"></p><h2 id="math类" tabindex="-1">Math类 <a class="header-anchor" href="#math类" aria-label="Permalink to &quot;Math类&quot;">​</a></h2><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">abs</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> a</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">     </span><span style="color:#676E95;font-style:italic;">// 返回参数的绝对值</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">double</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">ceil</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">double</span><span style="color:#BABED8;"> a</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">    </span><span style="color:#676E95;font-style:italic;">// 返回大于或等于参数的最小整数</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">double</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">floor</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">double</span><span style="color:#BABED8;"> a</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">   </span><span style="color:#676E95;font-style:italic;">// 返回小于或等于参数的最大整数</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">round</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">float</span><span style="color:#BABED8;"> a</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">    </span><span style="color:#676E95;font-style:italic;">// 按照四舍五入返回最接近参数的int类型的值</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">max</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> a</span><span style="color:#89DDFF;">,</span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> b</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">    </span><span style="color:#676E95;font-style:italic;">// 获取两个int值中的较大值</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">min</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> a</span><span style="color:#89DDFF;">,</span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> b</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">    </span><span style="color:#676E95;font-style:italic;">// 获取两个int值中的较小值</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">double</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">pow</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">double</span><span style="color:#BABED8;"> a</span><span style="color:#89DDFF;">,</span><span style="color:#C792EA;">double</span><span style="color:#BABED8;"> b</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#676E95;font-style:italic;">// 计算a的b次幂的值</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">double</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">random</span><span style="color:#89DDFF;">()</span><span style="color:#BABED8;">     </span><span style="color:#676E95;font-style:italic;">// 返回一个[0.0,1.0)的随机值</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h2 id="system类" tabindex="-1">System类 <a class="header-anchor" href="#system类" aria-label="Permalink to &quot;System类&quot;">​</a></h2><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">long</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">currentTimeMillis</span><span style="color:#89DDFF;">()</span><span style="color:#BABED8;">   </span><span style="color:#676E95;font-style:italic;">// 获取当前时间所对应的毫秒值（当前时间为0时区所对应的时间即就是英国格林尼治天文台旧址所在位置）</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">exit</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> status</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">    </span><span style="color:#676E95;font-style:italic;">// 终止当前正在运行的Java虚拟机，0表示正常退出，非零表示异常退出</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">native</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">arraycopy</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">Object</span><span style="color:#BABED8;"> src</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;">  </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;">  srcPos</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">Object</span><span style="color:#BABED8;"> dest</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> destPos</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> length</span><span style="color:#89DDFF;">);</span><span style="color:#BABED8;"> </span><span style="color:#676E95;font-style:italic;">// 进行数值元素copy</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h2 id="runtime" tabindex="-1">Runtime <a class="header-anchor" href="#runtime" aria-label="Permalink to &quot;Runtime&quot;">​</a></h2><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">Runtime</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">getRuntime</span><span style="color:#89DDFF;">()</span><span style="color:#BABED8;">  </span><span style="color:#676E95;font-style:italic;">//当前系统的运行环境对象</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">exit</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> status</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">   </span><span style="color:#676E95;font-style:italic;">//停止虚拟机</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">availableProcessors</span><span style="color:#89DDFF;">()</span><span style="color:#BABED8;">  </span><span style="color:#676E95;font-style:italic;">//获得CPU的线程数</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">long</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">maxMemory</span><span style="color:#89DDFF;">()</span><span style="color:#BABED8;">        </span><span style="color:#676E95;font-style:italic;">//JVM能从系统中获取总内存大小（单位byte）</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">long</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">totalMemory</span><span style="color:#89DDFF;">()</span><span style="color:#BABED8;">    </span><span style="color:#676E95;font-style:italic;">//JVM已经从系统中获取总内存大小（单位byte）</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">long</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">freeMemory</span><span style="color:#89DDFF;">()</span><span style="color:#BABED8;">    </span><span style="color:#676E95;font-style:italic;">//JVM剩余内存大小（单位byte）</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">Process</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">exec</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> command</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">  </span><span style="color:#676E95;font-style:italic;">//运行cmd命令</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h2 id="object类" tabindex="-1">Object类 <a class="header-anchor" href="#object类" aria-label="Permalink to &quot;Object类&quot;">​</a></h2><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">toString</span><span style="color:#89DDFF;">()</span><span style="color:#BABED8;">    </span><span style="color:#676E95;font-style:italic;">//返回该对象的字符串表示形式(可以看做是对象的内存地址值)</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">boolean</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">equals</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">Object</span><span style="color:#BABED8;"> obj</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">  </span><span style="color:#676E95;font-style:italic;">//比较两个对象地址值是否相等；true表示相同，false表示不相同</span></span>
<span class="line"><span style="color:#C792EA;">protected</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">Object</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">clone</span><span style="color:#89DDFF;">()</span><span style="color:#BABED8;">       </span><span style="color:#676E95;font-style:italic;">//对象克隆</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h2 id="objects类" tabindex="-1">Objects类 <a class="header-anchor" href="#objects类" aria-label="Permalink to &quot;Objects类&quot;">​</a></h2><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">toString</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">Object</span><span style="color:#BABED8;"> o</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">      </span><span style="color:#676E95;font-style:italic;">// 获取对象的字符串表现形式</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">boolean</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">equals</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">Object</span><span style="color:#BABED8;"> a</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">Object</span><span style="color:#BABED8;"> b</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">   </span><span style="color:#676E95;font-style:italic;">// 比较两个对象是否相等</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">boolean</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">isNull</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">Object</span><span style="color:#BABED8;"> obj</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">     </span><span style="color:#676E95;font-style:italic;">// 判断对象是否为null</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">boolean</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">nonNull</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">Object</span><span style="color:#BABED8;"> obj</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">     </span><span style="color:#676E95;font-style:italic;">// 判断对象是否不为null</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h2 id="biginteger类" tabindex="-1">BigInteger类 <a class="header-anchor" href="#biginteger类" aria-label="Permalink to &quot;BigInteger类&quot;">​</a></h2><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">BigInteger</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> num</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">Random</span><span style="color:#BABED8;"> rnd</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">   </span><span style="color:#676E95;font-style:italic;">//获取随机大整数，范围：[0 ~ 2的num次方-1]</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">BigInteger</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> val</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">     </span><span style="color:#676E95;font-style:italic;">//获取指定的大整数</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">BigInteger</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> val</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> radix</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">  </span><span style="color:#676E95;font-style:italic;">//获取指定进制的大整数</span></span>
<span class="line"><span style="color:#BABED8;">    </span></span>
<span class="line"><span style="color:#BABED8;">下面这个不是构造，而是一个静态方法获取BigInteger对象</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">BigInteger</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">valueOf</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">long</span><span style="color:#BABED8;"> val</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">  </span><span style="color:#676E95;font-style:italic;">//静态方法获取BigInteger的对象，内部有优化[-16到16]</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">BigInteger</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">BigInteger</span><span style="color:#BABED8;"> val</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">     </span><span style="color:#676E95;font-style:italic;">//加法</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">BigInteger</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">subtract</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">BigInteger</span><span style="color:#BABED8;"> val</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">    </span><span style="color:#676E95;font-style:italic;">//减法</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">BigInteger</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">multiply</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">BigInteger</span><span style="color:#BABED8;"> val</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">    </span><span style="color:#676E95;font-style:italic;">//乘法</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">BigInteger</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">divide</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">BigInteger</span><span style="color:#BABED8;"> val</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">    </span><span style="color:#676E95;font-style:italic;">//除法</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">BigInteger</span><span style="color:#89DDFF;">[]</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">divideAndRemainder</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">BigInteger</span><span style="color:#BABED8;"> val</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">  </span><span style="color:#676E95;font-style:italic;">//除法，获取商和余数</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;">  </span><span style="color:#C792EA;">boolean</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">equals</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">Object</span><span style="color:#BABED8;"> x</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">          </span><span style="color:#676E95;font-style:italic;">//比较是否相同</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;">  </span><span style="color:#C792EA;">BigInteger</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">pow</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> exponent</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">      </span><span style="color:#676E95;font-style:italic;">//次幂、次方</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;">  </span><span style="color:#C792EA;">BigInteger</span><span style="color:#BABED8;"> max</span><span style="color:#89DDFF;">/</span><span style="color:#82AAFF;">min</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">BigInteger</span><span style="color:#BABED8;"> val</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">     </span><span style="color:#676E95;font-style:italic;">//返回较大值/较小值</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;">  </span><span style="color:#C792EA;">int</span><span style="color:#89DDFF;">/</span><span style="color:#C792EA;">double</span><span style="color:#89DDFF;">/</span><span style="color:#C792EA;">long</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">intValue</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">BigInteger</span><span style="color:#BABED8;"> val</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">      </span><span style="color:#676E95;font-style:italic;">//转为int类型整数，超出范围数据有误</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h2 id="bigdecimal类" tabindex="-1">BigDecimal类 <a class="header-anchor" href="#bigdecimal类" aria-label="Permalink to &quot;BigDecimal类&quot;">​</a></h2><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">class</span><span style="color:#BABED8;"> </span><span style="color:#FFCB6B;">BigDecimalDemo01</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">main</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">[]</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">args</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">        System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">0.09</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">+</span><span style="color:#BABED8;"> </span><span style="color:#F78C6C;">0.01</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">0.09999999999999999</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>这样的结果其实就是一个丢失精度的结果。为什么会产生精度丢失呢？</p><p>在使用float或者double类型的数据在进行数学运算的时候，很有可能会产生精度丢失问题。我们都知道计算机底层在进行运算的时候，使用的都是二进制数据； 当我们在程序中写了一个十进制数据 ，在</p><p>进行运算的时候，计算机会将这个十进制数据转换成二进制数据，然后再进行运算，计算完毕以后计算机会把运算的结果再转换成十进制数据给我们展示； 如果我们使用的是整数类型的数据进行计算，那</p><p>么在把十进制数据转换成二进制数据的时候不会存在精度问题； 如果我们的数据是一个浮点类型的数据，有的时候计算机并不会将这个数据完全转换成一个二进制数据，而是将这个将其转换成一个无限的</p><p>趋近于这个十进数的二进制数据； 这样使用一个不太准确的数据进行运算的时候， 最终就会造成精度丢失；为了提高精度，Java就给我们提供了BigDecimal供我们进行数据运算。</p><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">BigDecimal</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">BigDecimal</span><span style="color:#BABED8;"> value</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">    </span><span style="color:#676E95;font-style:italic;">// 加法运算</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">BigDecimal</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">subtract</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">BigDecimal</span><span style="color:#BABED8;"> value</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">  </span><span style="color:#676E95;font-style:italic;">// 减法运算</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">BigDecimal</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">multiply</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">BigDecimal</span><span style="color:#BABED8;"> value</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">  </span><span style="color:#676E95;font-style:italic;">// 乘法运算</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">BigDecimal</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">divide</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">BigDecimal</span><span style="color:#BABED8;"> value</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;">   </span><span style="color:#676E95;font-style:italic;">// 触发运算</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">BigDecimal</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">divide</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">BigDecimal</span><span style="color:#BABED8;"> divisor</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> scale</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> roundingMode</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">divisor</span><span style="color:#89DDFF;font-style:italic;">:</span><span style="color:#BABED8;">   除数对应的BigDecimal对象；</span></span>
<span class="line"><span style="color:#BABED8;">scale</span><span style="color:#89DDFF;font-style:italic;">:</span><span style="color:#BABED8;">    精确的位数；</span></span>
<span class="line"><span style="color:#BABED8;">roundingMode</span><span style="color:#89DDFF;font-style:italic;">:</span><span style="color:#BABED8;">  取舍模式；</span></span>
<span class="line"><span style="color:#BABED8;">取舍模式被封装到了RoundingMode这个枚举类中（关于枚举我们后期再做重点讲解），在这个枚举类中定义了很多种取舍方式。最常见的取舍方式有如下几个：</span></span>
<span class="line"><span style="color:#82AAFF;">UP</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">远离零方向（原点）取舍</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> ，</span><span style="color:#82AAFF;">DOWN</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">靠近原点取舍</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span></span>
<span class="line"><span style="color:#BABED8;">GEILING（向正方向）取舍 ，FLOOR（向负方向取舍）</span></span>
<span class="line"><span style="color:#82AAFF;">HALF_UP</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">4舍五入</span><span style="color:#89DDFF;">),</span><span style="color:#BABED8;">我们可以通过如下格式直接访问这些取舍模式</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h1 id="集合" tabindex="-1">集合 <a class="header-anchor" href="#集合" aria-label="Permalink to &quot;集合&quot;">​</a></h1><h2 id="arraylist集合" tabindex="-1">ArrayList集合 <a class="header-anchor" href="#arraylist集合" aria-label="Permalink to &quot;ArrayList集合&quot;">​</a></h2><p><img src="/scroll-kings-notes/assets/1708084820829.0b242c4f.png" alt="70808482082" data-fancybox="gallery"></p><h2 id="collection" tabindex="-1">Collection <a class="header-anchor" href="#collection" aria-label="Permalink to &quot;Collection&quot;">​</a></h2><p><img src="/scroll-kings-notes/assets/1708084902760.59a630ac.png" alt="70808490276" data-fancybox="gallery"></p><h2 id="迭代器" tabindex="-1">迭代器 <a class="header-anchor" href="#迭代器" aria-label="Permalink to &quot;迭代器&quot;">​</a></h2><ul><li><p>Iterator中的常用方法</p><p>​ boolean hasNext(): 判断当前位置是否有元素可以被取出 E next(): 获取当前位置的元素,将迭代器对象移向下一个索引位置</p></li></ul><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">class</span><span style="color:#BABED8;"> </span><span style="color:#FFCB6B;">IteratorDemo1</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">main</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">[]</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">args</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">          </span><span style="color:#676E95;font-style:italic;">//创建集合对象</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#C792EA;">Collection</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">&gt;</span><span style="color:#BABED8;"> c </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">ArrayList</span><span style="color:#89DDFF;">&lt;&gt;();</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">          </span><span style="color:#676E95;font-style:italic;">//添加元素</span></span>
<span class="line"><span style="color:#BABED8;">          c</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">hello</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">          c</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">world</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">          c</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">java</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">          c</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">javaee</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">          </span><span style="color:#676E95;font-style:italic;">//Iterator&lt;E&gt; iterator()：返回此集合中元素的迭代器，通过集合的iterator()方法得到</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#C792EA;">Iterator</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">&gt;</span><span style="color:#BABED8;"> it </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> c</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">iterator</span><span style="color:#89DDFF;">();</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">          </span><span style="color:#676E95;font-style:italic;">//用while循环改进元素的判断和获取</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#89DDFF;font-style:italic;">while</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">it</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">hasNext</span><span style="color:#89DDFF;">())</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">              </span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> s </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> it</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">next</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#BABED8;">              System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">s</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">  </span><span style="color:#89DDFF;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><div class="language- line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#babed8;">- 迭代器中删除的方法</span></span>
<span class="line"><span style="color:#babed8;"></span></span>
<span class="line"><span style="color:#babed8;">  ​ void remove(): 删除迭代器对象当前指向的元素</span></span>
<span class="line"><span style="color:#babed8;"></span></span>
<span class="line"><span style="color:#babed8;">```java</span></span>
<span class="line"><span style="color:#babed8;">  public class IteratorDemo2 {</span></span>
<span class="line"><span style="color:#babed8;">      public static void main(String[] args) {</span></span>
<span class="line"><span style="color:#babed8;">          ArrayList&lt;String&gt; list = new ArrayList&lt;&gt;();</span></span>
<span class="line"><span style="color:#babed8;">          list.add(&quot;a&quot;);</span></span>
<span class="line"><span style="color:#babed8;">          list.add(&quot;b&quot;);</span></span>
<span class="line"><span style="color:#babed8;">          list.add(&quot;b&quot;);</span></span>
<span class="line"><span style="color:#babed8;">          list.add(&quot;c&quot;);</span></span>
<span class="line"><span style="color:#babed8;">          list.add(&quot;d&quot;);</span></span>
<span class="line"><span style="color:#babed8;"></span></span>
<span class="line"><span style="color:#babed8;">          Iterator&lt;String&gt; it = list.iterator();</span></span>
<span class="line"><span style="color:#babed8;">          while(it.hasNext()){</span></span>
<span class="line"><span style="color:#babed8;">              String s = it.next();</span></span>
<span class="line"><span style="color:#babed8;">              if(&quot;b&quot;.equals(s)){</span></span>
<span class="line"><span style="color:#babed8;">                  //指向谁,那么此时就删除谁.</span></span>
<span class="line"><span style="color:#babed8;">                  it.remove();</span></span>
<span class="line"><span style="color:#babed8;">              }</span></span>
<span class="line"><span style="color:#babed8;">          }</span></span>
<span class="line"><span style="color:#babed8;">          System.out.println(list);</span></span>
<span class="line"><span style="color:#babed8;">      }</span></span>
<span class="line"><span style="color:#babed8;">  }</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><h2 id="增强for循环【应用】" tabindex="-1">增强for循环【应用】 <a class="header-anchor" href="#增强for循环【应用】" aria-label="Permalink to &quot;增强for循环【应用】&quot;">​</a></h2><blockquote><p><code>arr.for</code></p></blockquote><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">class</span><span style="color:#BABED8;"> </span><span style="color:#FFCB6B;">MyCollectonDemo1</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">main</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">[]</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">args</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#C792EA;">ArrayList</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">&gt;</span><span style="color:#BABED8;"> list </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;">  </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">ArrayList</span><span style="color:#89DDFF;">&lt;&gt;();</span></span>
<span class="line"><span style="color:#BABED8;">        list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">a</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">b</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">c</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">d</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">e</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">f</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;">//1,数据类型一定是集合或者数组中元素的类型</span></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;">//2,str仅仅是一个变量名而已,在循环的过程中,依次表示集合或者数组中的每一个元素</span></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;">//3,list就是要遍历的集合或者数组</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> str </span><span style="color:#89DDFF;font-style:italic;">:</span><span style="color:#BABED8;"> list</span><span style="color:#89DDFF;">){</span></span>
<span class="line"><span style="color:#BABED8;">            System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">str</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><h2 id="foreach" tabindex="-1">forEach <a class="header-anchor" href="#foreach" aria-label="Permalink to &quot;forEach&quot;">​</a></h2><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">import</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">java</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">util</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">ArrayList</span><span style="color:#89DDFF;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">class</span><span style="color:#BABED8;"> </span><span style="color:#FFCB6B;">Test</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">main</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">[]</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">args</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#C792EA;">ArrayList</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">&gt;</span><span style="color:#BABED8;"> list </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">ArrayList</span><span style="color:#89DDFF;">&lt;&gt;();</span></span>
<span class="line"><span style="color:#BABED8;">        list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">a</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">b</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">c</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">d</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">e</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">f</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;">//1,数据类型一定是集合或者数组中元素的类型</span></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;">//2,str仅仅是一个变量名而已,在循环的过程中,依次表示集合或者数组中的每一个元素</span></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;">//3,list就是要遍历的集合或者数组</span></span>
<span class="line"><span style="color:#BABED8;">        list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">forEach</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">s </span><span style="color:#C792EA;">-&gt;</span><span style="color:#BABED8;"> System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">s</span><span style="color:#89DDFF;">));</span></span>
<span class="line"><span style="color:#BABED8;">        list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">forEach</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;font-style:italic;">::</span><span style="color:#BABED8;">println</span><span style="color:#89DDFF;">);</span><span style="color:#676E95;font-style:italic;">// 后面学</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><h2 id="list集合" tabindex="-1">List集合 <a class="header-anchor" href="#list集合" aria-label="Permalink to &quot;List集合&quot;">​</a></h2><p><img src="/scroll-kings-notes/assets/1708086099853.21467c14.png" alt="70808609985" data-fancybox="gallery"></p><h2 id="linklist集合" tabindex="-1">LinkList集合 <a class="header-anchor" href="#linklist集合" aria-label="Permalink to &quot;LinkList集合&quot;">​</a></h2><p><img src="/scroll-kings-notes/assets/1708086429889.47a9c4bf.png" alt="70808642988" data-fancybox="gallery"></p><h2 id="set集合" tabindex="-1">Set集合 <a class="header-anchor" href="#set集合" aria-label="Permalink to &quot;Set集合&quot;">​</a></h2><ul><li>不可以存储重复元素</li><li>没有索引,不能使用普通for循环遍历</li></ul><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">class</span><span style="color:#BABED8;"> </span><span style="color:#FFCB6B;">MySet1</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">main</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">[]</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">args</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">       </span><span style="color:#676E95;font-style:italic;">//创建集合对象</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#C792EA;">Set</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">&gt;</span><span style="color:#BABED8;"> set </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">TreeSet</span><span style="color:#89DDFF;">&lt;&gt;();</span></span>
<span class="line"><span style="color:#89DDFF;">       </span><span style="color:#676E95;font-style:italic;">//添加元素</span></span>
<span class="line"><span style="color:#BABED8;">        set</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">ccc</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        set</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">aaa</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        set</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">aaa</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        set</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">bbb</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">//        for (int i = 0; i &lt; set.size(); i++) {</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">//            //Set集合是没有索引的，所以不能使用通过索引获取元素的方法</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">//        }</span></span>
<span class="line"><span style="color:#BABED8;">      </span></span>
<span class="line"><span style="color:#89DDFF;">       </span><span style="color:#676E95;font-style:italic;">//遍历集合</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#C792EA;">Iterator</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">&gt;</span><span style="color:#BABED8;"> it </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> set</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">iterator</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#89DDFF;font-style:italic;">while</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">it</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">hasNext</span><span style="color:#89DDFF;">()){</span></span>
<span class="line"><span style="color:#BABED8;">            </span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> s </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> it</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">next</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#BABED8;">            System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">s</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">        System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">-----------------------------------</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> s </span><span style="color:#89DDFF;font-style:italic;">:</span><span style="color:#BABED8;"> set</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">            System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">s</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br></div></div><h2 id="treeset集合" tabindex="-1">TreeSet集合 <a class="header-anchor" href="#treeset集合" aria-label="Permalink to &quot;TreeSet集合&quot;">​</a></h2><h3 id="treeset集合概述和特点【应用】" tabindex="-1">TreeSet集合概述和特点【应用】 <a class="header-anchor" href="#treeset集合概述和特点【应用】" aria-label="Permalink to &quot;TreeSet集合概述和特点【应用】&quot;">​</a></h3><ul><li>不可以存储重复元素</li><li>没有索引</li><li>可以将元素按照规则进行排序 <ul><li>TreeSet()：根据其元素的自然排序进行排序</li><li>TreeSet(Comparator comparator) ：根据指定的比较器进行排序</li></ul></li></ul><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">class</span><span style="color:#BABED8;"> </span><span style="color:#FFCB6B;">TreeSetDemo01</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">main</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">[]</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">args</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;">//创建集合对象</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#C792EA;">TreeSet</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">Integer</span><span style="color:#89DDFF;">&gt;</span><span style="color:#BABED8;"> ts </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">TreeSet</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">Integer</span><span style="color:#89DDFF;">&gt;();</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;">//添加元素</span></span>
<span class="line"><span style="color:#BABED8;">        ts</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">10</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        ts</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">40</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        ts</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">30</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        ts</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">50</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        ts</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">20</span><span style="color:#89DDFF;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">        ts</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">30</span><span style="color:#89DDFF;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;">//遍历集合</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">Integer</span><span style="color:#BABED8;"> i </span><span style="color:#89DDFF;font-style:italic;">:</span><span style="color:#BABED8;"> ts</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">            System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">i</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h2 id="map集合" tabindex="-1">Map集合 <a class="header-anchor" href="#map集合" aria-label="Permalink to &quot;Map集合&quot;">​</a></h2><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">interface</span><span style="color:#BABED8;"> </span><span style="color:#FFCB6B;">Map</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">K</span><span style="color:#89DDFF;">,</span><span style="color:#C792EA;">V</span><span style="color:#89DDFF;">&gt;</span><span style="color:#BABED8;">  K：键的类型；V：值的类型</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><ul><li><p>双列集合,一个键对应一个值</p></li><li><p>键不可以重复,值可以重复</p></li><li><p>Map集合的基本使用</p></li></ul><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">class</span><span style="color:#BABED8;"> </span><span style="color:#FFCB6B;">MapDemo01</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">main</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">[]</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">args</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">          </span><span style="color:#676E95;font-style:italic;">//创建集合对象</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#C792EA;">Map</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">,</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">&gt;</span><span style="color:#BABED8;"> map </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">HashMap</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">,</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">&gt;();</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">          </span><span style="color:#676E95;font-style:italic;">//V put(K key, V value) 将指定的值与该映射中的指定键相关联</span></span>
<span class="line"><span style="color:#BABED8;">          map</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">put</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">itheima001</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">林青霞</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">          map</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">put</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">itheima002</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">张曼玉</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">          map</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">put</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">itheima003</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">王祖贤</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">          map</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">put</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">itheima003</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">柳岩</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">          </span><span style="color:#676E95;font-style:italic;">//输出集合对象</span></span>
<span class="line"><span style="color:#BABED8;">          System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">map</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">  </span><span style="color:#89DDFF;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h3 id="_1-2map集合的基本功能【应用】" tabindex="-1">1.2Map集合的基本功能【应用】 <a class="header-anchor" href="#_1-2map集合的基本功能【应用】" aria-label="Permalink to &quot;1.2Map集合的基本功能【应用】&quot;">​</a></h3><ul><li><p>方法介绍</p><table><thead><tr><th>方法名</th><th>说明</th></tr></thead><tbody><tr><td>V put(K key,V value)</td><td>添加元素</td></tr><tr><td>V remove(Object key)</td><td>根据键删除键值对元素</td></tr><tr><td>void clear()</td><td>移除所有的键值对元素</td></tr><tr><td>boolean containsKey(Object key)</td><td>判断集合是否包含指定的键</td></tr><tr><td>boolean containsValue(Object value)</td><td>判断集合是否包含指定的值</td></tr><tr><td>boolean isEmpty()</td><td>判断集合是否为空</td></tr><tr><td>int size()</td><td>集合的长度，也就是集合中键值对的个数</td></tr></tbody></table></li></ul><h3 id="_1-3map集合的获取功能【应用】" tabindex="-1">1.3Map集合的获取功能【应用】 <a class="header-anchor" href="#_1-3map集合的获取功能【应用】" aria-label="Permalink to &quot;1.3Map集合的获取功能【应用】&quot;">​</a></h3><ul><li><p>方法介绍</p><table><thead><tr><th>方法名</th><th>说明</th></tr></thead><tbody><tr><td>V get(Object key)</td><td>根据键获取值</td></tr><tr><td>SetK keySet()</td><td>获取所有键的集合</td></tr><tr><td>CollectionV values()</td><td>获取所有值的集合</td></tr><tr><td>Set&lt;Map.Entry&lt;K,V&gt;&gt; entrySet()</td><td>获取所有键值对对象的集合</td></tr></tbody></table></li></ul><h3 id="_1-4map集合的遍历-方式1-【应用】" tabindex="-1">1.4Map集合的遍历(方式1)【应用】 <a class="header-anchor" href="#_1-4map集合的遍历-方式1-【应用】" aria-label="Permalink to &quot;1.4Map集合的遍历(方式1)【应用】&quot;">​</a></h3><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">class</span><span style="color:#BABED8;"> </span><span style="color:#FFCB6B;">MapDemo01</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">main</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">[]</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">args</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;">//创建集合对象</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#C792EA;">Map</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">&gt;</span><span style="color:#BABED8;"> map </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">HashMap</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">&gt;();</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;">//添加元素</span></span>
<span class="line"><span style="color:#BABED8;">        map</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">put</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">张无忌</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">赵敏</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        map</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">put</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">郭靖</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">黄蓉</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        map</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">put</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">杨过</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">小龙女</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;">//获取所有键的集合。用keySet()方法实现</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#C792EA;">Set</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">&gt;</span><span style="color:#BABED8;"> keySet </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> map</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">keySet</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;">//遍历键的集合，获取到每一个键。用增强for实现</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> key </span><span style="color:#89DDFF;font-style:italic;">:</span><span style="color:#BABED8;"> keySet</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">            </span><span style="color:#676E95;font-style:italic;">//根据键去找值。用get(Object key)方法实现</span></span>
<span class="line"><span style="color:#BABED8;">            </span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> value </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> map</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">get</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">key</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">            System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">key </span><span style="color:#89DDFF;">+</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">+</span><span style="color:#BABED8;"> value</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h3 id="_1-5map集合的遍历-方式2-【应用】" tabindex="-1">1.5Map集合的遍历(方式2)【应用】 <a class="header-anchor" href="#_1-5map集合的遍历-方式2-【应用】" aria-label="Permalink to &quot;1.5Map集合的遍历(方式2)【应用】&quot;">​</a></h3><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">class</span><span style="color:#BABED8;"> </span><span style="color:#FFCB6B;">MapDemo02</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">main</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">[]</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">args</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;">//创建集合对象</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#C792EA;">Map</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">&gt;</span><span style="color:#BABED8;"> map </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">HashMap</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">&gt;();</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;">//添加元素</span></span>
<span class="line"><span style="color:#BABED8;">        map</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">put</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">张无忌</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">赵敏</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        map</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">put</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">郭靖</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">黄蓉</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        map</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">put</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">杨过</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">小龙女</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;">//获取所有键值对对象的集合</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#C792EA;">Set</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">Map</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">Entry</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">&gt;&gt;</span><span style="color:#BABED8;"> entrySet </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> map</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">entrySet</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;">//遍历键值对对象的集合，得到每一个键值对对象</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">Map</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">Entry</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">&gt;</span><span style="color:#BABED8;"> me </span><span style="color:#89DDFF;font-style:italic;">:</span><span style="color:#BABED8;"> entrySet</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">            </span><span style="color:#676E95;font-style:italic;">//根据键值对对象获取键和值</span></span>
<span class="line"><span style="color:#BABED8;">            </span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> key </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> me</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">getKey</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#BABED8;">            </span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> value </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> me</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">getValue</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#BABED8;">            System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">key </span><span style="color:#89DDFF;">+</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">+</span><span style="color:#BABED8;"> value</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><h2 id="hashmap集合" tabindex="-1">HashMap集合 <a class="header-anchor" href="#hashmap集合" aria-label="Permalink to &quot;HashMap集合&quot;">​</a></h2><h3 id="_2-1hashmap集合概述和特点【理解】" tabindex="-1">2.1HashMap集合概述和特点【理解】 <a class="header-anchor" href="#_2-1hashmap集合概述和特点【理解】" aria-label="Permalink to &quot;2.1HashMap集合概述和特点【理解】&quot;">​</a></h3><ul><li>HashMap底层是哈希表结构的</li><li>依赖hashCode方法和equals方法保证键的唯一</li><li>如果键要存储的是自定义对象，需要重写hashCode和equals方法</li><li>存入的数据无序排列</li></ul><h3 id="_2-2hashmap集合应用案例【应用】" tabindex="-1">2.2HashMap集合应用案例【应用】 <a class="header-anchor" href="#_2-2hashmap集合应用案例【应用】" aria-label="Permalink to &quot;2.2HashMap集合应用案例【应用】&quot;">​</a></h3><ul><li><p>案例需求</p><ul><li>创建一个HashMap集合，键是学生对象(Student)，值是居住地 (String)。存储多个元素，并遍历。</li><li>要求保证键的唯一性：如果学生对象的成员变量值相同，我们就认为是同一个对象</li></ul></li><li><p>代码实现</p><p>学生类</p></li></ul><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">class</span><span style="color:#BABED8;"> </span><span style="color:#FFCB6B;">Student</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">private</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> name</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">private</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> age</span><span style="color:#89DDFF;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">Student</span><span style="color:#89DDFF;">()</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">Student</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">name</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">age</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#89DDFF;">this.</span><span style="color:#BABED8;">name </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> name</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#89DDFF;">this.</span><span style="color:#BABED8;">age </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> age</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">getName</span><span style="color:#89DDFF;">()</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#89DDFF;font-style:italic;">return</span><span style="color:#BABED8;"> name</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">setName</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">name</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#89DDFF;">this.</span><span style="color:#BABED8;">name </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> name</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">getAge</span><span style="color:#89DDFF;">()</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#89DDFF;font-style:italic;">return</span><span style="color:#BABED8;"> age</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">setAge</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">age</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#89DDFF;">this.</span><span style="color:#BABED8;">age </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> age</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Override</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">boolean</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">equals</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">Object</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">o</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">(this</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">==</span><span style="color:#BABED8;"> o</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">return</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">true;</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">o </span><span style="color:#89DDFF;">==</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">null</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">||</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">getClass</span><span style="color:#89DDFF;">()</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">!=</span><span style="color:#BABED8;"> o</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">getClass</span><span style="color:#89DDFF;">())</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">return</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">false;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#C792EA;">Student</span><span style="color:#BABED8;"> student </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">Student</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> o</span><span style="color:#89DDFF;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">age </span><span style="color:#89DDFF;">!=</span><span style="color:#BABED8;"> student</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">age</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">return</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">false;</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#89DDFF;font-style:italic;">return</span><span style="color:#BABED8;"> name </span><span style="color:#89DDFF;">!=</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">null</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">?</span><span style="color:#BABED8;"> name</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">equals</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">student</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">name</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">:</span><span style="color:#BABED8;"> student</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">name </span><span style="color:#89DDFF;">==</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">null;</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Override</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">hashCode</span><span style="color:#89DDFF;">()</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> result </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> name </span><span style="color:#89DDFF;">!=</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">null</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">?</span><span style="color:#BABED8;"> name</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">hashCode</span><span style="color:#89DDFF;">()</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">:</span><span style="color:#BABED8;"> </span><span style="color:#F78C6C;">0</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">          result </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#F78C6C;">31</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">*</span><span style="color:#BABED8;"> result </span><span style="color:#89DDFF;">+</span><span style="color:#BABED8;"> age</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#89DDFF;font-style:italic;">return</span><span style="color:#BABED8;"> result</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">  </span><span style="color:#89DDFF;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br></div></div><p>测试类</p><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">class</span><span style="color:#BABED8;"> </span><span style="color:#FFCB6B;">HashMapDemo</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">main</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">[]</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">args</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">          </span><span style="color:#676E95;font-style:italic;">//创建HashMap集合对象</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#C792EA;">HashMap</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">Student</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">&gt;</span><span style="color:#BABED8;"> hm </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">HashMap</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">Student</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">&gt;();</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">          </span><span style="color:#676E95;font-style:italic;">//创建学生对象</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#C792EA;">Student</span><span style="color:#BABED8;"> s1 </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">Student</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">林青霞</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#F78C6C;">30</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#C792EA;">Student</span><span style="color:#BABED8;"> s2 </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">Student</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">张曼玉</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#F78C6C;">35</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#C792EA;">Student</span><span style="color:#BABED8;"> s3 </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">Student</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">王祖贤</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#F78C6C;">33</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#C792EA;">Student</span><span style="color:#BABED8;"> s4 </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">Student</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">王祖贤</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#F78C6C;">33</span><span style="color:#89DDFF;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">          </span><span style="color:#676E95;font-style:italic;">//把学生添加到集合</span></span>
<span class="line"><span style="color:#BABED8;">          hm</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">put</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">s1</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">西安</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">          hm</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">put</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">s2</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">武汉</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">          hm</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">put</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">s3</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">郑州</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">          hm</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">put</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">s4</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">北京</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">          </span><span style="color:#676E95;font-style:italic;">//遍历集合</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#C792EA;">Set</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">Student</span><span style="color:#89DDFF;">&gt;</span><span style="color:#BABED8;"> keySet </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> hm</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">keySet</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">Student</span><span style="color:#BABED8;"> key </span><span style="color:#89DDFF;font-style:italic;">:</span><span style="color:#BABED8;"> keySet</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">              </span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> value </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> hm</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">get</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">key</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">              System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">key</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">getName</span><span style="color:#89DDFF;">()</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">+</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">+</span><span style="color:#BABED8;"> key</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">getAge</span><span style="color:#89DDFF;">()</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">+</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">+</span><span style="color:#BABED8;"> value</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">  </span><span style="color:#89DDFF;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><h2 id="treemap集合" tabindex="-1">TreeMap集合 <a class="header-anchor" href="#treemap集合" aria-label="Permalink to &quot;TreeMap集合&quot;">​</a></h2><h3 id="_3-1treemap集合概述和特点【理解】" tabindex="-1">3.1TreeMap集合概述和特点【理解】 <a class="header-anchor" href="#_3-1treemap集合概述和特点【理解】" aria-label="Permalink to &quot;3.1TreeMap集合概述和特点【理解】&quot;">​</a></h3><ul><li>TreeMap底层是红黑树结构</li><li>依赖自然排序或者比较器排序,对键进行排序</li><li>如果键存储的是自定义对象,需要实现Comparable接口或者在创建TreeMap对象时候给出比较器排序规则</li></ul><h3 id="_3-2treemap集合应用案例【应用】" tabindex="-1">3.2TreeMap集合应用案例【应用】 <a class="header-anchor" href="#_3-2treemap集合应用案例【应用】" aria-label="Permalink to &quot;3.2TreeMap集合应用案例【应用】&quot;">​</a></h3><ul><li><p>案例需求</p><ul><li>创建一个TreeMap集合,键是学生对象(Student),值是籍贯(String),学生属性姓名和年龄,按照年龄进行排序并遍历</li><li>要求按照学生的年龄进行排序,如果年龄相同则按照姓名进行排序</li></ul></li><li><p>代码实现</p><p>学生类</p></li></ul><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">class</span><span style="color:#BABED8;"> </span><span style="color:#FFCB6B;">Student</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">implements</span><span style="color:#BABED8;"> </span><span style="color:#FFCB6B;">Comparable</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">Student</span><span style="color:#89DDFF;">&gt;{</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">private</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> name</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">private</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> age</span><span style="color:#89DDFF;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">Student</span><span style="color:#89DDFF;">()</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">Student</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">name</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">age</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#89DDFF;">this.</span><span style="color:#BABED8;">name </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> name</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#89DDFF;">this.</span><span style="color:#BABED8;">age </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> age</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">getName</span><span style="color:#89DDFF;">()</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#89DDFF;font-style:italic;">return</span><span style="color:#BABED8;"> name</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">setName</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">name</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#89DDFF;">this.</span><span style="color:#BABED8;">name </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> name</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">getAge</span><span style="color:#89DDFF;">()</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#89DDFF;font-style:italic;">return</span><span style="color:#BABED8;"> age</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">setAge</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">age</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#89DDFF;">this.</span><span style="color:#BABED8;">age </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> age</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Override</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">toString</span><span style="color:#89DDFF;">()</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#89DDFF;font-style:italic;">return</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Student{</span><span style="color:#89DDFF;">&quot;</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">+</span></span>
<span class="line"><span style="color:#BABED8;">                  </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">name=&#39;</span><span style="color:#89DDFF;">&quot;</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">+</span><span style="color:#BABED8;"> name </span><span style="color:#89DDFF;">+</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#BABED8;">\&#39;</span><span style="color:#89DDFF;">&#39;</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">+</span></span>
<span class="line"><span style="color:#BABED8;">                  </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">, age=</span><span style="color:#89DDFF;">&quot;</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">+</span><span style="color:#BABED8;"> age </span><span style="color:#89DDFF;">+</span></span>
<span class="line"><span style="color:#BABED8;">                  </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">}</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Override</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">compareTo</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">Student</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">o</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">          </span><span style="color:#676E95;font-style:italic;">//按照年龄进行排序</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> result </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> o</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">getAge</span><span style="color:#89DDFF;">()</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">-</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">this.</span><span style="color:#82AAFF;">getAge</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#89DDFF;">          </span><span style="color:#676E95;font-style:italic;">//次要条件，按照姓名排序。</span></span>
<span class="line"><span style="color:#BABED8;">          result </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> result </span><span style="color:#89DDFF;">==</span><span style="color:#BABED8;"> </span><span style="color:#F78C6C;">0</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">?</span><span style="color:#BABED8;"> o</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">getName</span><span style="color:#89DDFF;">().</span><span style="color:#82AAFF;">compareTo</span><span style="color:#89DDFF;">(this.</span><span style="color:#82AAFF;">getName</span><span style="color:#89DDFF;">())</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">:</span><span style="color:#BABED8;"> result</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#89DDFF;font-style:italic;">return</span><span style="color:#BABED8;"> result</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">  </span><span style="color:#89DDFF;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br></div></div><p>测试类</p><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">class</span><span style="color:#BABED8;"> </span><span style="color:#FFCB6B;">Test1</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">main</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">[]</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">args</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">         </span><span style="color:#676E95;font-style:italic;">// 创建TreeMap集合对象</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#C792EA;">TreeMap</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">Student</span><span style="color:#89DDFF;">,</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">&gt;</span><span style="color:#BABED8;"> tm </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">TreeMap</span><span style="color:#89DDFF;">&lt;&gt;();</span></span>
<span class="line"><span style="color:#BABED8;">        </span></span>
<span class="line"><span style="color:#89DDFF;">    </span><span style="color:#676E95;font-style:italic;">// 创建学生对象</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#C792EA;">Student</span><span style="color:#BABED8;"> s1 </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">Student</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">xiaohei</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#F78C6C;">23</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#C792EA;">Student</span><span style="color:#BABED8;"> s2 </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">Student</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">dapang</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#F78C6C;">22</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#C792EA;">Student</span><span style="color:#BABED8;"> s3 </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">Student</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">xiaomei</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#F78C6C;">22</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        </span></span>
<span class="line"><span style="color:#89DDFF;">    </span><span style="color:#676E95;font-style:italic;">// 将学生对象添加到TreeMap集合中</span></span>
<span class="line"><span style="color:#BABED8;">          tm</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">put</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">s1</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">江苏</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">          tm</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">put</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">s2</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">北京</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">          tm</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">put</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">s3</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">天津</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        </span></span>
<span class="line"><span style="color:#89DDFF;">    </span><span style="color:#676E95;font-style:italic;">// 遍历TreeMap集合,打印每个学生的信息</span></span>
<span class="line"><span style="color:#BABED8;">          tm</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">forEach</span><span style="color:#89DDFF;">(</span></span>
<span class="line"><span style="color:#BABED8;">                  </span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">Student</span><span style="color:#BABED8;"> key</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> value</span><span style="color:#89DDFF;">)</span><span style="color:#C792EA;">-&gt;</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">                      System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">key </span><span style="color:#89DDFF;">+</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">---</span><span style="color:#89DDFF;">&quot;</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">+</span><span style="color:#BABED8;"> value</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">                  </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">  </span><span style="color:#89DDFF;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><h2 id="可变参数" tabindex="-1">可变参数 <a class="header-anchor" href="#可变参数" aria-label="Permalink to &quot;可变参数&quot;">​</a></h2><p>在<strong>JDK1.5</strong>之后，如果我们定义一个方法需要接受多个参数，并且多个参数类型一致，我们可以对其简化.</p><p><strong>格式：</strong></p><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#BABED8;">修饰符 返回值类型 方法名</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">参数类型</span><span style="color:#89DDFF;">...</span><span style="color:#BABED8;"> 形参名</span><span style="color:#89DDFF;">){</span><span style="color:#BABED8;">  </span><span style="color:#89DDFF;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><strong>底层：</strong></p><p>​ 其实就是一个数组</p><p><strong>好处：</strong></p><p>​ 在传递数据的时候，省的我们自己创建数组并添加元素了，JDK底层帮我们自动创建数组并添加元素了</p><p><strong>代码演示:</strong></p><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">class</span><span style="color:#BABED8;"> </span><span style="color:#FFCB6B;">ChangeArgs</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">main</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">[]</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">args</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> sum </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">getSum</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">6</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#F78C6C;">7</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#F78C6C;">2</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#F78C6C;">12</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#F78C6C;">2121</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">sum</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">    </span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">getSum</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">int</span><span style="color:#89DDFF;">...</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">arr</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">     </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> sum </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#F78C6C;">0</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">         </span><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> a </span><span style="color:#89DDFF;font-style:italic;">:</span><span style="color:#BABED8;"> arr</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">         sum </span><span style="color:#89DDFF;">+=</span><span style="color:#BABED8;"> a</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;font-style:italic;">return</span><span style="color:#BABED8;"> sum</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p><strong>注意：</strong></p><p>​ 1.一个方法只能有一个可变参数</p><p>​ 2.如果方法中有多个参数，可变参数要放到最后。</p><p>​ 在Collections中也提供了添加一些元素方法：</p><p>​ <code>public static &lt;T&gt; boolean addAll(Collection&lt;T&gt; c, T... elements)</code>:往集合中添加一些元素。</p><p><strong>代码演示:</strong></p><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">class</span><span style="color:#BABED8;"> </span><span style="color:#FFCB6B;">CollectionsDemo</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;"> </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">main</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">[]</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">args</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">ArrayList</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">Integer</span><span style="color:#89DDFF;">&gt;</span><span style="color:#BABED8;"> list </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">ArrayList</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">Integer</span><span style="color:#89DDFF;">&gt;();</span></span>
<span class="line"><span style="color:#89DDFF;">      </span><span style="color:#676E95;font-style:italic;">//原来写法</span></span>
<span class="line"><span style="color:#89DDFF;">      </span><span style="color:#676E95;font-style:italic;">//list.add(12);</span></span>
<span class="line"><span style="color:#89DDFF;">      </span><span style="color:#676E95;font-style:italic;">//list.add(14);</span></span>
<span class="line"><span style="color:#89DDFF;">      </span><span style="color:#676E95;font-style:italic;">//list.add(15);</span></span>
<span class="line"><span style="color:#89DDFF;">      </span><span style="color:#676E95;font-style:italic;">//list.add(1000);</span></span>
<span class="line"><span style="color:#89DDFF;">      </span><span style="color:#676E95;font-style:italic;">//采用工具类 完成 往集合中添加元素  </span></span>
<span class="line"><span style="color:#BABED8;">      Collections</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">addAll</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">list</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#F78C6C;">5</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#F78C6C;">222</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#F78C6C;">1</span><span style="color:#BABED8;">，</span><span style="color:#F78C6C;">2</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">      System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">list</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h2 id="collections类" tabindex="-1">Collections类 <a class="header-anchor" href="#collections类" aria-label="Permalink to &quot;Collections类&quot;">​</a></h2><p><img src="/scroll-kings-notes/assets/1708096659325.1504f192.png" alt="70809665932" data-fancybox="gallery"></p><h3 id="collections常用功能" tabindex="-1">Collections常用功能 <a class="header-anchor" href="#collections常用功能" aria-label="Permalink to &quot;Collections常用功能&quot;">​</a></h3><ul><li><p><code>java.utils.Collections</code>是集合工具类，用来对集合进行操作。</p><p>常用方法如下：</p></li><li><p><code>public static void shuffle(List&lt;?&gt; list)</code>:打乱集合顺序。</p></li><li><p><code>public static &lt;T&gt; void sort(List&lt;T&gt; list)</code>:将集合中元素按照默认规则排序。</p></li><li><p><code>public static &lt;T&gt; void sort(List&lt;T&gt; list，Comparator&lt;? super T&gt; )</code>:将集合中元素按照指定规则排序。</p></li></ul><p>代码演示：</p><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">class</span><span style="color:#BABED8;"> </span><span style="color:#FFCB6B;">CollectionsDemo</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">main</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">[]</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">args</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#C792EA;">ArrayList</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">Integer</span><span style="color:#89DDFF;">&gt;</span><span style="color:#BABED8;"> list </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">ArrayList</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">Integer</span><span style="color:#89DDFF;">&gt;();</span></span>
<span class="line"><span style="color:#BABED8;">   </span></span>
<span class="line"><span style="color:#BABED8;">        list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">100</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">300</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">200</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">50</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;">//排序方法 </span></span>
<span class="line"><span style="color:#BABED8;">        Collections</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">sort</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">list</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">list</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">结果：</span></span>
<span class="line"><span style="color:#89DDFF;">[</span><span style="color:#F78C6C;">50</span><span style="color:#89DDFF;">,</span><span style="color:#F78C6C;">100</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#F78C6C;">200</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#F78C6C;">300</span><span style="color:#89DDFF;">]</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>我们的集合按照默认的自然顺序进行了排列，如果想要指定顺序那该怎么办呢？</p><h3 id="comparator比较器" tabindex="-1">Comparator比较器 <a class="header-anchor" href="#comparator比较器" aria-label="Permalink to &quot;Comparator比较器&quot;">​</a></h3><p>创建一个学生类，存储到ArrayList集合中完成指定排序操作。</p><p>Student 类</p><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">class</span><span style="color:#BABED8;"> </span><span style="color:#FFCB6B;">Student</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#C792EA;">private</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> name</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#C792EA;">private</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> age</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;"> </span><span style="color:#676E95;font-style:italic;">//构造方法</span></span>
<span class="line"><span style="color:#89DDFF;">    </span><span style="color:#676E95;font-style:italic;">//get/set</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">//toString</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>测试类：</p><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">class</span><span style="color:#BABED8;"> </span><span style="color:#FFCB6B;">Demo</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">main</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">[]</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">args</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;">// 创建四个学生对象 存储到集合中</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#C792EA;">ArrayList</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">Student</span><span style="color:#89DDFF;">&gt;</span><span style="color:#BABED8;"> list </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">ArrayList</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">Student</span><span style="color:#89DDFF;">&gt;();</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">        list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">Student</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">rose</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#F78C6C;">18</span><span style="color:#89DDFF;">));</span></span>
<span class="line"><span style="color:#BABED8;">        list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">Student</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">jack</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#F78C6C;">16</span><span style="color:#89DDFF;">));</span></span>
<span class="line"><span style="color:#BABED8;">        list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">Student</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">abc</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#F78C6C;">20</span><span style="color:#89DDFF;">));</span></span>
<span class="line"><span style="color:#BABED8;">  Collections</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">sort</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">list</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">Comparator</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">Student</span><span style="color:#89DDFF;">&gt;()</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Override</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">compare</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">Student</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">o1</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">Student</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">o2</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">         </span><span style="color:#89DDFF;font-style:italic;">return</span><span style="color:#BABED8;"> o1</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">getAge</span><span style="color:#89DDFF;">()-</span><span style="color:#BABED8;">o2</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">getAge</span><span style="color:#89DDFF;">();</span><span style="color:#676E95;font-style:italic;">//以学生的年龄升序</span></span>
<span class="line"><span style="color:#BABED8;">      </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">  </span><span style="color:#89DDFF;">});</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">Student</span><span style="color:#BABED8;"> student </span><span style="color:#89DDFF;font-style:italic;">:</span><span style="color:#BABED8;"> list</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">            System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">student</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">Student</span><span style="color:#89DDFF;">{</span><span style="color:#BABED8;">name</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">jack</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> age</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">16</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">Student</span><span style="color:#89DDFF;">{</span><span style="color:#BABED8;">name</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">rose</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> age</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">18</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">Student</span><span style="color:#89DDFF;">{</span><span style="color:#BABED8;">name</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">abc</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> age</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">20</span><span style="color:#89DDFF;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><h2 id="不可变集合" tabindex="-1">不可变集合 <a class="header-anchor" href="#不可变集合" aria-label="Permalink to &quot;不可变集合&quot;">​</a></h2><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">class</span><span style="color:#BABED8;"> </span><span style="color:#FFCB6B;">ImmutableDemo1</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">main</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">[]</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">args</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#676E95;font-style:italic;">/*</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">            创建不可变的List集合</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">            &quot;张三&quot;, &quot;李四&quot;, &quot;王五&quot;, &quot;赵六&quot;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">        */</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;">//一旦创建完毕之后，是无法进行修改的，在下面的代码中，只能进行查询操作</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#C792EA;">List</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">&gt;</span><span style="color:#BABED8;"> list </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> List</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">of</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">张三</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">李四</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">王五</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">赵六</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">        System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">get</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">0</span><span style="color:#89DDFF;">));</span></span>
<span class="line"><span style="color:#BABED8;">        System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">get</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">));</span></span>
<span class="line"><span style="color:#BABED8;">        System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">get</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">2</span><span style="color:#89DDFF;">));</span></span>
<span class="line"><span style="color:#BABED8;">        System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">get</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">3</span><span style="color:#89DDFF;">));</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">        System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">---------------------------</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> s </span><span style="color:#89DDFF;font-style:italic;">:</span><span style="color:#BABED8;"> list</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">            System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">s</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">        System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">---------------------------</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#C792EA;">Iterator</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">&gt;</span><span style="color:#BABED8;"> it </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">iterator</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#89DDFF;font-style:italic;">while</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">it</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">hasNext</span><span style="color:#89DDFF;">()){</span></span>
<span class="line"><span style="color:#BABED8;">            </span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> s </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> it</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">next</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#BABED8;">            System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">s</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">        System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">---------------------------</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> i </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#F78C6C;">0</span><span style="color:#89DDFF;">;</span><span style="color:#BABED8;"> i </span><span style="color:#89DDFF;">&lt;</span><span style="color:#BABED8;"> list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">size</span><span style="color:#89DDFF;">();</span><span style="color:#BABED8;"> i</span><span style="color:#89DDFF;">++)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">            </span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> s </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">get</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">i</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">            System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">s</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">        System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">---------------------------</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;">//list.remove(&quot;李四&quot;);</span></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;">//list.add(&quot;aaa&quot;);</span></span>
<span class="line"><span style="color:#BABED8;">        list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">set</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">0</span><span style="color:#89DDFF;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">aaa</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br></div></div><h2 id="stream流" tabindex="-1">Stream流 <a class="header-anchor" href="#stream流" aria-label="Permalink to &quot;Stream流&quot;">​</a></h2><table><thead><tr><th style="text-align:left;">单列集合：list.stream().forEach(s -&gt; System.out.println(s)😉</th></tr></thead><tbody><tr><td style="text-align:left;"><strong>双列集合（1）：hm.keySet().stream().forEach(s -System.out.println(s));</strong></td></tr><tr><td style="text-align:left;"><strong>双列集合（2）：hm.entrySet().stream().forEach(s-&gt;System.out.println(s));</strong></td></tr><tr><td style="text-align:left;"><strong>数组：Arrays.stream(arr2).forEach(s-&gt;System.out.println(s));</strong></td></tr><tr><td style="text-align:left;"><strong>零散数据：Stream.of(1,2,3,4,5).forEach(s-&gt;System.out.println(s));</strong></td></tr></tbody></table><p><code>常见方法</code></p><p><img src="/scroll-kings-notes/assets/1708102401532.b4ff5430.png" alt="70810240153" data-fancybox="gallery"></p><blockquote><p><code>Stream.concat(list1.stream(),list2.stream());</code></p></blockquote><p>终结操作的意思是,执行完此方法之后,Stream流将不能再执行其他操作</p><table><thead><tr><th>方法名</th><th>说明</th></tr></thead><tbody><tr><td>void forEach(Consumer action)</td><td>对此流的每个元素执行操作</td></tr><tr><td>long count()</td><td>返回此流中的元素数</td></tr><tr><td>toArray(value -&gt; new String[value])</td><td>放到数组中 Arrays.toString(arr)</td></tr></tbody></table><ul><li><p>概念</p><p>对数据使用Stream流的方式操作完毕后,可以把流中的数据收集到集合中</p></li><li><p>常用方法</p><table><thead><tr><th>方法名</th><th>说明</th></tr></thead><tbody><tr><td>R collect(Collector collector)</td><td>把结果收集到集合中</td></tr></tbody></table></li><li><p>工具类Collectors提供了具体的收集方式</p><table><thead><tr><th>方法名</th><th>说明</th></tr></thead><tbody><tr><td>public static T Collector toList()</td><td>把元素收集到List集合中</td></tr><tr><td>public static T Collector toSet() <code>去重</code></td><td>把元素收集到Set集合中</td></tr><tr><td>public static Collector toMap(Function keyMapper,Function valueMapper)</td><td>把元素收集到Map集合中</td></tr></tbody></table></li><li><p>代码演示</p></li></ul><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">class</span><span style="color:#BABED8;"> </span><span style="color:#FFCB6B;">MyStream8</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">   </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">main</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">[]</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">args</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">         </span><span style="color:#C792EA;">ArrayList</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">&gt;</span><span style="color:#BABED8;"> list </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">ArrayList</span><span style="color:#89DDFF;">&lt;&gt;();</span></span>
<span class="line"><span style="color:#BABED8;">          list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">zhangsan,23</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">          list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">lisi,24</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">          list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">add</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">wangwu,25</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#C792EA;">Map</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">Integer</span><span style="color:#89DDFF;">&gt;</span><span style="color:#BABED8;"> map </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">stream</span><span style="color:#89DDFF;">().</span><span style="color:#82AAFF;">filter</span><span style="color:#89DDFF;">(</span></span>
<span class="line"><span style="color:#BABED8;">                  s </span><span style="color:#C792EA;">-&gt;</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">                      </span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">[]</span><span style="color:#BABED8;"> split </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> s</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">split</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">                      </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> age </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> Integer</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">parseInt</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">split</span><span style="color:#89DDFF;">[</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">]);</span></span>
<span class="line"><span style="color:#BABED8;">                      </span><span style="color:#89DDFF;font-style:italic;">return</span><span style="color:#BABED8;"> age </span><span style="color:#89DDFF;">&gt;=</span><span style="color:#BABED8;"> </span><span style="color:#F78C6C;">24</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">                  </span><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">           </span><span style="color:#676E95;font-style:italic;">//   collect方法只能获取到流中剩余的每一个数据.</span></span>
<span class="line"><span style="color:#89DDFF;">           </span><span style="color:#676E95;font-style:italic;">//在底层不能创建容器,也不能把数据添加到容器当中</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">           </span><span style="color:#676E95;font-style:italic;">//Collectors.toMap 创建一个map集合并将数据添加到集合当中</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">            </span><span style="color:#676E95;font-style:italic;">// s 依次表示流中的每一个数据</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">            </span><span style="color:#676E95;font-style:italic;">//第一个lambda表达式就是如何获取到Map中的键</span></span>
<span class="line"><span style="color:#89DDFF;">            </span><span style="color:#676E95;font-style:italic;">//第二个lambda表达式就是如何获取Map中的值</span></span>
<span class="line"><span style="color:#BABED8;">          </span><span style="color:#89DDFF;">).</span><span style="color:#82AAFF;">collect</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">Collectors</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">toMap</span><span style="color:#89DDFF;">(</span></span>
<span class="line"><span style="color:#BABED8;">                  s </span><span style="color:#C792EA;">-&gt;</span><span style="color:#BABED8;"> s</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">split</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">)[</span><span style="color:#F78C6C;">0</span><span style="color:#89DDFF;">],</span></span>
<span class="line"><span style="color:#BABED8;">                  s </span><span style="color:#C792EA;">-&gt;</span><span style="color:#BABED8;"> Integer</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">parseInt</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">s</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">split</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">)[</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">])</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">));</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">          System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">map</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">   </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">  </span><span style="color:#89DDFF;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br></div></div><h2 id="方法引用" tabindex="-1">方法引用 <a class="header-anchor" href="#方法引用" aria-label="Permalink to &quot;方法引用&quot;">​</a></h2><blockquote><p><strong>方法引用和sout</strong></p></blockquote><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">import</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">java</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">util</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">ArrayList</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#F78C6C;">import</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">java</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">util</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">Collections</span><span style="color:#89DDFF;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">class</span><span style="color:#BABED8;"> </span><span style="color:#FFCB6B;">Test</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">main</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">[]</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">args</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#C792EA;">ArrayList</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">&gt;</span><span style="color:#BABED8;"> list </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">ArrayList</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">&gt;();</span></span>
<span class="line"><span style="color:#BABED8;">        Collections</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">addAll</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">list</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Apple</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Banana</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Cherry</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Date</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        Collections</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">shuffle</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">list</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">stream</span><span style="color:#89DDFF;">().</span><span style="color:#82AAFF;">filter</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">Test</span><span style="color:#89DDFF;font-style:italic;">::</span><span style="color:#BABED8;">A</span><span style="color:#89DDFF;">).</span><span style="color:#82AAFF;">forEach</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;font-style:italic;">::</span><span style="color:#BABED8;">println</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">boolean</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">A</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">s</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#89DDFF;font-style:italic;">return</span><span style="color:#BABED8;"> s</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">startsWith</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">A</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><blockquote><p><strong>Integer::parseInt</strong> <strong>Integer[]::new</strong></p></blockquote><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">import</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">java</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">util</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">ArrayList</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#F78C6C;">import</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">java</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">util</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">Collections</span><span style="color:#89DDFF;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">class</span><span style="color:#BABED8;"> </span><span style="color:#FFCB6B;">Test</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#C792EA;">public</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">static</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">void</span><span style="color:#BABED8;"> </span><span style="color:#82AAFF;">main</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">[]</span><span style="color:#BABED8;"> </span><span style="color:#BABED8;font-style:italic;">args</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#C792EA;">ArrayList</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">&gt;</span><span style="color:#BABED8;"> list </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#BABED8;"> </span><span style="color:#C792EA;">ArrayList</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">String</span><span style="color:#89DDFF;">&gt;();</span></span>
<span class="line"><span style="color:#BABED8;">        Collections</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">addAll</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">list</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">11</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">22</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">33</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">44</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        Collections</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">shuffle</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">list</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#C792EA;">Integer</span><span style="color:#89DDFF;">[]</span><span style="color:#BABED8;"> arr </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> list</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">stream</span><span style="color:#89DDFF;">().</span><span style="color:#82AAFF;">map</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">Integer</span><span style="color:#89DDFF;font-style:italic;">::</span><span style="color:#BABED8;">parseInt</span><span style="color:#89DDFF;">).</span><span style="color:#82AAFF;">toArray</span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">Integer</span><span style="color:#89DDFF;">[]</span><span style="color:#89DDFF;font-style:italic;">::new</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#C792EA;">int</span><span style="color:#BABED8;"> sum </span><span style="color:#89DDFF;">=</span><span style="color:#BABED8;"> </span><span style="color:#F78C6C;">0</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">Integer</span><span style="color:#BABED8;"> integer </span><span style="color:#89DDFF;font-style:italic;">:</span><span style="color:#BABED8;"> arr</span><span style="color:#89DDFF;">)</span><span style="color:#BABED8;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#BABED8;">            System</span><span style="color:#89DDFF;">.</span><span style="color:#BABED8;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#BABED8;">sum</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#BABED8;">            sum </span><span style="color:#89DDFF;">+=</span><span style="color:#BABED8;"> integer</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#BABED8;">        </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#BABED8;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><blockquote><p>String::toUpperCase 转大写</p></blockquote></div></div></main><footer class="VPDocFooter" data-v-79724263 data-v-0d6c8703><!--[--><!--]--><div class="edit-info" data-v-0d6c8703><!----><div class="last-updated" data-v-0d6c8703><p class="VPLastUpdated" data-v-0d6c8703 data-v-d32addb7>Last updated: <time datetime="2024-02-17T16:38:11.000Z" data-v-d32addb7></time></p></div></div><nav class="prev-next" data-v-0d6c8703><div class="pager" data-v-0d6c8703><!----></div><div class="pager" data-v-0d6c8703><a class="pager-link next" href="/scroll-kings-notes/javaweb.html" data-v-0d6c8703><span class="desc" data-v-0d6c8703>下一篇</span><span class="title" data-v-0d6c8703>JavaWeb</span></a></div></nav></footer><!--[--><!--]--></div></div></div><!--[--><!--]--></div></div><footer class="VPFooter has-sidebar" data-v-88c5167e data-v-73bc1bc4><div class="container" data-v-73bc1bc4><p class="message" data-v-73bc1bc4>根据 MIT 许可证发布</p><p class="copyright" data-v-73bc1bc4> Copyright © 2023-2024 YPS</p></div></footer><!--[--><!--]--></div></div>
    <script>window.__VP_HASH_MAP__=JSON.parse("{\"readme.md\":\"c87a555e\",\"life.md\":\"9a144f21\",\"web.md\":\"509b7fef\",\"mybatisplus.md\":\"9e7099a1\",\"javaweb.md\":\"5680debd\",\"javase.md\":\"05ebba13\",\"index.md\":\"b5507705\",\"cangqiong.md\":\"6082b9cf\"}");window.__VP_SITE_DATA__=JSON.parse("{\"lang\":\"zh-CN\",\"dir\":\"ltr\",\"title\":\"王遂杨卷死你哼！\",\"titleTemplate\":\"╭(╯^╰)╮\",\"description\":\"王遂杨的卷王日常\",\"base\":\"/scroll-kings-notes/\",\"head\":[],\"appearance\":true,\"themeConfig\":{\"logo\":\"/wsy.png\",\"siteTitle\":\"天不生我王遂杨卷道万古如长夜\",\"returnToTopLabel\":\"返回顶部\",\"search\":{\"provider\":\"local\",\"options\":{\"translations\":{\"button\":{\"buttonText\":\"搜索文档\",\"buttonAriaLabel\":\"搜索文档\"},\"modal\":{\"displayDetails\":\"展开详情\",\"noResultsText\":\"无法找到相关结果\",\"resetButtonTitle\":\"清除查询条件\",\"footer\":{\"selectText\":\"选择\",\"navigateText\":\"切换\"}}}}},\"outline\":{\"label\":\"目录\",\"level\":\"deep\"},\"docFooter\":{\"prev\":\"上一篇\",\"next\":\"下一篇\"},\"footer\":{\"message\":\"根据 MIT 许可证发布\",\"copyright\":\" Copyright © 2023-2024 YPS\"},\"nav\":[{\"text\":\"卷王笔记ヾ(•ω•`)o\",\"items\":[{\"text\":\"JavaSE\",\"link\":\"/javase\"},{\"text\":\"JavaWeb\",\"link\":\"/javaweb\"},{\"text\":\"前端\",\"link\":\"/web\"},{\"text\":\"苍穹外卖\",\"link\":\"/cangqiong\"},{\"text\":\"MybatisPlus\",\"link\":\"/mybatisplus\"}]},{\"text\":\"卷王日常(￣o￣).zZ\",\"link\":\"/life\"}],\"sidebar\":[{\"text\":\"看我王遂杨卷死你\",\"items\":[{\"text\":\"JavaSE\",\"link\":\"/javase\"},{\"text\":\"JavaWeb\",\"link\":\"/javaweb\"},{\"text\":\"前端\",\"link\":\"/web\"},{\"text\":\"苍穹外卖\",\"link\":\"/cangqiong\"},{\"text\":\"MybatisPlus\",\"link\":\"/mybatisplus\"}]}],\"socialLinks\":[{\"icon\":{\"svg\":\"<svg t=\\\"1708159775126\\\" class=\\\"icon\\\" viewBox=\\\"0 0 1024 1024\\\" version=\\\"1.1\\\" xmlns=\\\"http://www.w3.org/2000/svg\\\" p-id=\\\"10613\\\" width=\\\"200\\\" height=\\\"200\\\"><path d=\\\"M669.3 369.4c9.8 0 19.6 0 29.4 1.6C671 245.2 536.9 152 383.2 152 211.6 152 71 269.7 71 416.8c0 85 45.8 156.9 124.2 210.9l-31.1 93.2L273.6 667c39.2 8.2 70.3 16.3 109.5 16.3 9.8 0 19.6 0 31.1-1.6-6.5-21.3-9.8-42.5-9.8-65.4 0.1-135.7 116.2-246.9 264.9-246.9z m-168.4-85c24.5 0 39.2 16.3 39.2 39.2 0 22.9-16.3 39.2-39.2 39.2-24.5 0-47.4-16.4-47.4-39.2 0-24.5 24.6-39.2 47.4-39.2z m-216.3 73.1c-24.7 0-47.8-16.2-47.8-38.8 0-24.3 24.7-38.8 47.8-38.8s39.5 16.2 39.5 38.8c0.1 22.7-16.4 38.8-39.5 38.8z\\\" fill=\\\"#24DB5A\\\" p-id=\\\"10614\\\"></path><path d=\\\"M953.8 613c0-125.9-124.2-227.2-264.8-227.2-148.8 0-266.5 103-266.5 227.2 0 125.9 117.7 227.2 266.5 227.2 31.1 0 62.1-8.2 93.2-16.3l85 47.4-22.9-78.5c62.1-47.4 109.5-109.5 109.5-179.8z m-351.5-39.2c-14.7 0-31.1-14.7-31.1-31.1 0-14.7 16.3-31.1 31.1-31.1 22.9 0 39.2 16.3 39.2 31.1 0 16.4-14.7 31.1-39.2 31.1z m178-7.6c-14.8 0-31.3-14.6-31.3-30.7 0-14.6 16.5-30.7 31.3-30.7 23.1 0 39.5 16.2 39.5 30.7 0 16.2-16.4 30.7-39.5 30.7z\\\" fill=\\\"#24DB5A\\\" p-id=\\\"10615\\\"></path></svg>\"},\"link\":\"\"},{\"icon\":{\"svg\":\"<svg t=\\\"1708159621014\\\" class=\\\"icon\\\" viewBox=\\\"0 0 1024 1024\\\" version=\\\"1.1\\\" xmlns=\\\"http://www.w3.org/2000/svg\\\" p-id=\\\"8757\\\" width=\\\"200\\\" height=\\\"200\\\"><path d=\\\"M512 1024C229.222 1024 0 794.778 0 512S229.222 0 512 0s512 229.222 512 512-229.222 512-512 512z m259.149-568.883h-290.74a25.293 25.293 0 0 0-25.292 25.293l-0.026 63.206c0 13.952 11.315 25.293 25.267 25.293h177.024c13.978 0 25.293 11.315 25.293 25.267v12.646a75.853 75.853 0 0 1-75.853 75.853h-240.23a25.293 25.293 0 0 1-25.267-25.293V417.203a75.853 75.853 0 0 1 75.827-75.853h353.946a25.293 25.293 0 0 0 25.267-25.292l0.077-63.207a25.293 25.293 0 0 0-25.268-25.293H417.152a189.62 189.62 0 0 0-189.62 189.645V771.15c0 13.977 11.316 25.293 25.294 25.293h372.94a170.65 170.65 0 0 0 170.65-170.65V480.384a25.293 25.293 0 0 0-25.293-25.267z\\\" fill=\\\"#C71D23\\\" p-id=\\\"8758\\\"></path></svg>\"},\"link\":\"\"},{\"icon\":{\"svg\":\"<svg t=\\\"1708159909604\\\" class=\\\"icon\\\" viewBox=\\\"0 0 1024 1024\\\" version=\\\"1.1\\\" xmlns=\\\"http://www.w3.org/2000/svg\\\" p-id=\\\"12483\\\" width=\\\"200\\\" height=\\\"200\\\"><path d=\\\"M512 0a512 512 0 1 0 512 512A512 512 0 0 0 512 0z m256.717 460.186a151.962 151.962 0 0 1-87.347 65.74 83.251 83.251 0 0 1-24.474 4.096 29.082 29.082 0 0 1 0-58.163 15.667 15.667 0 0 0 6.451-1.229 91.443 91.443 0 0 0 55.91-40.96 75.264 75.264 0 0 0 11.06-39.628c0-45.978-42.496-83.866-94.31-83.866a105.267 105.267 0 0 0-51.2 13.414 81.92 81.92 0 0 0-43.725 70.452v244.224a138.445 138.445 0 0 1-72.704 120.422 159.642 159.642 0 0 1-79.77 20.48c-84.378 0-153.6-63.488-153.6-142.029a136.192 136.192 0 0 1 19.763-69.837 151.962 151.962 0 0 1 87.347-65.74 85.914 85.914 0 0 1 24.474-4.096 29.082 29.082 0 1 1 0 58.163 15.667 15.667 0 0 0-6.451 1.229 95.949 95.949 0 0 0-55.91 40.96 75.264 75.264 0 0 0-11.06 39.628c0 45.978 42.496 83.866 94.925 83.866a105.267 105.267 0 0 0 51.2-13.414 81.92 81.92 0 0 0 43.622-70.452V390.35a138.752 138.752 0 0 1 72.807-120.525 151.245 151.245 0 0 1 79.155-21.504c84.378 0 153.6 63.488 153.6 142.029a136.192 136.192 0 0 1-19.763 69.837z\\\" fill=\\\"#00B240\\\" p-id=\\\"12484\\\"></path></svg>\"},\"link\":\"\"}]},\"locales\":{},\"scrollOffset\":90,\"cleanUrls\":false}");</script>
    
  </body>
</html>